<?php
/**
 * Copyright 2011 MaGdev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * The names of the authors may not be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package      Mashape API-Documentor
 * @author       Marco Graetsch <info@magdev.de>
 * @copyright    2011 Marco Graetsch.development
 * @license      http://opensource.org/licenses/bsd-license.php New BSD License
 * @link         https://code.google.com/p/apidocumentor/
 */


if (!extension_loaded('xsl')) {
    throw new RuntimeException('XSL extension is required to run Mashape-Documentor');
}
if (extension_loaded('redland')) {
    throw new RuntimeException('redland extension must be disabled to run Mashape-Documentor. See https://bugs.php.net/bug.php?id=52257 for further information');
}

/**
 * Mashape API-Documentor XSL renderer
 *
 * @author magdev
 */
class Mashdoc_Renderer_Xsl extends Mashdoc_Renderer_Abstract
{
	/** @var XSLTProcessor */
	protected $_proc = null;
	
	/** @var DOMDocument */
	protected $_xsl = null;
	
	
	/**
	 * Constructor
	 *
	 * @param DOMDocument $xml
	 * @param DOMDocument $xsl
	 * @return void
	 */
	public function __construct(DOMDocument $xml = null, DOMDocument $xsl = null)
	{
		parent::__construct($xml);
		$this->_proc = new XSLTProcessor();
		
		if (!is_null($xsl)) {
			$this->setXsl($xsl);
		}
	}
	
	
	/**
	 * Set the XSL document
	 *
	 * @param DOMDocument $xsl
	 * @return Mashdoc_Renderer_Xsl
	 */
	public function setXsl(DOMDocument $xsl)
	{
		$this->_xsl = $xsl;
		return $this;
	}
	
	
	/**
	 * Load a XSL stylesheet
	 *
	 * @param string $file
	 * @return Mashdoc_Renderer_Xsl
	 */
	public function loadXsl($file)
	{
		$this->_xsl = new DOMDocument('1.0', $this->_options['encoding']);
		$this->_xsl->load($file);
		return $this;
	}
	
	
	/**
	 * Render the documentation
	 *
	 * @see Mashdoc_Renderer_Interface::render()
	 * @return Mashdoc_Renderer_Xsl
	 */
	public function render()
	{
	    if (is_null($this->_xsl)) {
    		$this->loadXsl($this->_options['xslFile']);
	    }
		$this->_proc->setParameter('', $this->_getParameters());
		$this->_proc->importStylesheet($this->_xsl);
		return $this;
	}
	
	
	/**
	 * Get the rendered output as string
	 *
	 * @return string
	 * @see Mashdoc_Renderer_Interface::__toString()
	 */
	public function __toString()
	{
	    if (is_null($this->_html)) {
    		$this->render();
    		$this->_html = '<!DOCTYPE '.$this->_options['doctype'].'>'.PHP_EOL.$this->_proc->transformToXml($this->_xml);
	    }
	    return $this->_html;
	}
}